﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;

namespace PlanoAulaHelper
{
    public partial class DiasEHorarioEnsinoForm : FormEdicao
    {
        public DiasEHorarioEnsinoForm(int idPlanoAulaCab) : base(idPlanoAulaCab)
        {
            InitializeComponent();
        }
        int horaInicio=0 , horaFim =0 , minutoInicio =0, minutoFim = 0;
        private void dgvConsulta_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {

            switch (e.ColumnIndex)
            {
                case 1:
                    {
                        if (e.Value != null)
                            e.Value = CultureInfo.CurrentCulture.DateTimeFormat.DayNames[e.Value.To<int>()];
                        break;
                    }
                case 2:
                    {

                        horaInicio = e.Value.To<int>();
                        break;
                    }
                case 3:
                    {
                        horaInicio = e.Value.To<int>();

                        minutoInicio = e.Value.To<int>();
                        break;
                    }
                case 4:
                    {
                        horaFim = e.Value.To<int>();
                        break;
                    }
                case 5:
                    {
                        minutoFim = e.Value.To<int>();
                        break;
                    }
                case 6:
                    {
                        horaInicio = dgvConsulta.Rows[e.RowIndex].Cells[2].Value.To<int>();
                        minutoInicio = dgvConsulta.Rows[e.RowIndex].Cells[3].Value.To<int>();
                        horaFim = dgvConsulta.Rows[e.RowIndex].Cells[4].Value.To<int>();
                        minutoFim = dgvConsulta.Rows[e.RowIndex].Cells[5].Value.To<int>();
                        e.Value = string.Format("{0}:{1}", horaInicio.ToString().PadLeft(2, '0'), minutoInicio.ToString().PadLeft(2, '0'));
                        break;
                    }
                case 7:
                    {
                        e.Value = string.Format("{0}:{1}", horaFim.ToString().PadLeft(2, '0'), minutoFim.ToString().PadLeft(2, '0'));
                        break;
                    }
            }
        
        }

        private void novoToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripButtonNovo_Click(sender, e);
        }

        private void habilitarManutencao(bool habilitar)
        {
            gbxEdicao.Visible = habilitar;
            dgvConsulta.Enabled = !habilitar;
        }

        private void preencherCampos(DiaAula diaAula)
        {
            if (diaAula.Dia > -1 && diaAula.Dia < 7)
                cmbDia.SelectedItem = Enum.Parse(typeof(DayOfWeek), diaAula.Dia.ToString());
            dtpDe.Value = new DateTime(1901, 01, 01, diaAula.HoraInicio, diaAula.MinutoInicio, 00);
            dtpAte.Value = new DateTime(1901, 01, 01, diaAula.HoraFinal, diaAula.MinutoFinal, 00);
        }

        private void dtpDe_ValueChanged(object sender, EventArgs e)
        {
            if (dtpAte.Value < dtpDe.Value)
            {
                dtpAte.Value = dtpDe.Value.AddMinutes(140);
            }
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            habilitarManutencao(false);
        }
        int idSelecionado = 0;
        private void Salvar()
        {
            using (dbContext db = new dbContext())
            {
                var dia = (from d in db.DiaAulas
                           where d.Id == idSelecionado
                           select d).SingleOrDefault();
                if (dia == null)
                {
                    dia = new DiaAula();
                    db.DiaAulas.InsertOnSubmit(dia);
                }
                dia.Dia = (int)((DayOfWeek)cmbDia.SelectedItem);

                dia.HoraFinal = dtpAte.Value.Hour;
                dia.HoraInicio = dtpDe.Value.Hour;
                dia.MinutoInicio = dtpDe.Value.Minute;
                dia.MinutoFinal = dtpAte.Value.Minute;
                dia.IdPlanoAulaCab = IdItem;


                db.SubmitChanges();
                habilitarManutencao(false);

            }
            AtualizarGrid();
        }

        private void AtualizarGrid()
        {
            using (dbContext db = new dbContext())
            {
                diaAulaBindingSource.DataSource = (from d in db.DiaAulas
                                                   where d.IdPlanoAulaCab == IdItem
                                                   orderby d.Dia ascending, d.HoraInicio ascending
                                                   select d);
            }
        }
        private void btnSalvar_Click(object sender, EventArgs e)
        {
            Salvar();
        }

        private void modificarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (diaAulaBindingSource.Current != null)
            {
                idSelecionado = ((DiaAula)diaAulaBindingSource.Current).Id;

                using (dbContext db = new dbContext())
                {
                    var dia = (from d in db.DiaAulas
                               where d.Id == idSelecionado
                               select d).Single();
                    preencherCampos(dia);
                }
                habilitarManutencao(true);
            }
        }

        private void dgvConsulta_DoubleClick(object sender, EventArgs e)
        {
            modificarToolStripMenuItem_Click(sender, e);
        }

        private void excluirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (diaAulaBindingSource.Current != null && MessageBox.Show("Confirma a exclusão do item?", "Excluir", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                idSelecionado = ((DiaAula)diaAulaBindingSource.Current).Id;
                using (dbContext db = new dbContext())
                {
                    var dia = (from d in db.DiaAulas
                               where d.Id == idSelecionado
                               select d).Single();
                    if (dia != null)
                    {
                        db.DiaAulas.DeleteOnSubmit(dia);
                    }
                    db.SubmitChanges();
                }
                AtualizarGrid();
            }
        }

        private void DiasEHorarioEnsinoForm_Load(object sender, EventArgs e)
        {
            cmbDia.DataSource = Enum.GetValues(typeof(DayOfWeek));
            AtualizarGrid();
            habilitarManutencao(false);

        }

        private void cmbDia_Format(object sender, ListControlConvertEventArgs e)
        {
            e.Value = CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)((DayOfWeek)e.Value)];
        }

        private void toolStripButtonNovo_Click(object sender, EventArgs e)
        {
            dgvConsulta.ClearSelection();
            habilitarManutencao(true);
            preencherCampos(new DiaAula());
        }
    }
}
